package cn.blunce.viewexercises.views;

import android.content.Context;
import android.view.View;

/**
 * Created by Experiment on 2016/7/19 0019.
 */
public class CustomView1 extends View {

    private final int DEFAULT_WIDTH = 200;
    private final int DEFAULT_HEITHT = 150;

    public CustomView1(Context context) {
        super(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(measureLength(widthMeasureSpec, 0), measureLength(heightMeasureSpec, 1));
    }

    /**
     * @param measureSpec
     * @param tag:0->>width,1->>height
     * @return
     */
    private int measureLength(int measureSpec, int tag) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        switch (specMode) {
            case MeasureSpec.EXACTLY:
                return specSize;
            case MeasureSpec.UNSPECIFIED:
                return tag == 0 ? DEFAULT_WIDTH : DEFAULT_HEITHT;
            case MeasureSpec.AT_MOST:
                return Math.min(tag == 0 ? DEFAULT_WIDTH : DEFAULT_HEITHT, specSize);
            default:
                return tag == 0 ? DEFAULT_WIDTH : DEFAULT_HEITHT;
        }
    }


}
